package com.huitone.gddw.service.impl.worktable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.GateDef;
import com.huitone.gddw.dao.entity.GateSwtich;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IGateDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IGateDefService;
import com.huitone.gddw.service.IGateSwtichService;
import com.huitone.gddw.service.ILinkSendCorpService;
import com.huitone.gddw.service.ILinkSendProService;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.dto.GateDefDTO;
import com.huitone.gddw.service.monitoring.IUserGateSerivce;
import com.huitone.gddw.utils.SysLoginUtil;

/**
* @Title GateDefService
* @Package com.huitone.gddw.service.impl
* @Description TODO
* @author chengsiyue
* @date 2017年5月5日 上午11:00:37
* @version V1.0   
*/
@Service
public class GateDefService implements IGateDefService {

	@Autowired
	private IGateDefDao gateDefDao;
	
	@Autowired
	private IGateSwtichService gateSwtichService;
	
	@Autowired
	private ILinkSendProService linkSendProService;
	
	@Autowired
	private ILinkSendCorpService linkSendCorpService;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Autowired
	private IUserGateSerivce userGateSerivce;
	
	@Override
	public QueryPage<GateDefDTO> selectPage(QueryPage<GateDef> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gateLinkId", page.getParams().get("gateLinkId"));
		map.put("status", page.getParams().get("status"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<GateDefDTO> result = gateDefDao.selectPage(map);
		int totalCount = gateDefDao.selectCount(map);
		
		QueryPage<GateDefDTO> queryPage = new QueryPage<GateDefDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public GateDef selectByPrimaryKey(String gateLinkId) {
		return gateDefDao.selectByPrimaryKey(gateLinkId);
	}

	@Override
	public GateDefDTO toUpdate(String gateLinkId) {
		GateDef entity = this.selectByPrimaryKey(gateLinkId);
		GateDefDTO gateDefDTO = new GateDefDTO(entity);
		return gateDefDTO;
	}

	@Override
	public void save(GateDef entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		gateDefDao.save(entity);
		
		this.updateParamRec("保存短信网关通道");
		
		// 添加通道线路信息
		GateSwtich gateSwtich = new GateSwtich();
		gateSwtich.setGateLinkId(entity.getGateLinkId());
		gateSwtichService.save(gateSwtich);
	}

	@Override
	public void update(GateDef entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		gateDefDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新短信网关通道");
	}

	@Override
	public void delete(String gateLinkId) {
		gateSwtichService.deleteByGateLinkId(gateLinkId);
		gateDefDao.deleteByPrimaryKey(gateLinkId);
		
		Map<String, Object> params = new HashMap<>();
		params.put("gateLinkId", gateLinkId);
		userGateSerivce.delete(params);
		this.updateParamRec("删除短信网关通道");
	}

	@Override
	public List<GateDefDTO> getRecord(Map<String, Object> params) {
		List<GateDefDTO> list = gateDefDao.getRecord(params);
		return list;
	}

	@Override
	public List<Map<String, Object>> getGateLink(String status,String gateLinkName) {
		List<Map<String, Object>> list = gateDefDao.getGateLink(status,gateLinkName);
		return list;
	}

	@Override
	public void changeLinkSend(String gateLinkId, String provinces, String mobilCorps) {
		this.updateProvinces(gateLinkId, provinces);
		this.updateMobilCorps(gateLinkId, mobilCorps);
	}
	
	/**
	 * 更新通道可发送省份
	 * @param gateLinkId 通道ID
	 * @param provinces 多个省份
	 */
	private void updateProvinces(String gateLinkId, String provinces) {
		List<String> list = StringUtils.isBlank(provinces) ? new ArrayList<String>() : Arrays.asList(provinces.split(","));
		List<String> listSave = new ArrayList<String>();
		List<String> listDel = new ArrayList<String>();
		List<String> listAll = linkSendProService.getProByGateLinkId(gateLinkId);
		for (String s : list) {
			if (!listAll.contains(s)) {
				listSave.add(s);
			}
		}
		for (String s : listAll) {
			if (!list.contains(s)) {
				listDel.add(s);
			}
		}
		linkSendProService.updateByGateLinkId(gateLinkId, listSave, listDel);
	}
	
	/**
	 * 更新通道可发送运营商
	 * @param gateLinkId 通道ID
	 * @param provinces 多个运营商
	 */
	private void updateMobilCorps(String gateLinkId, String mobilCorps) {
		List<String> list = StringUtils.isBlank(mobilCorps) ? new ArrayList<String>() : Arrays.asList(mobilCorps.split(","));
		List<String> listSave = new ArrayList<String>();
		List<String> listDel = new ArrayList<String>();
		List<String> listAll = linkSendCorpService.getCorpByGateLinkId(gateLinkId);
		for (String s : list) {
			if (!listAll.contains(s)) {
				listSave.add(s);
			}
		}
		for (String s : listAll) {
			if (!list.contains(s)) {
				listDel.add(s);
			}
		}
		linkSendCorpService.updateByGateLinkId(gateLinkId, listSave, listDel);
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.GATE_DEF, desc);
	}

	@Override
	public void updateByPrimaryKeySelective(GateDef entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		gateDefDao.updateByPrimaryKeySelective(entity);
		
		this.updateParamRec("更新短信网关通道");
	}

	@Override
	public List<Map<String, Object>> getGateLinkByGateLinkName(String gateLinkName) {
		List<Map<String, Object>> list = gateDefDao.getGateLinkByGateLinkName(gateLinkName);
		return list;
	}

}
